package leetcode.editor.cn;

public class Solution654 {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return helper(nums, 0, nums.length-1);
    }

    TreeNode helper(int[] nums, int left, int right){
        if (left>right) return null;
        int index = -1, max = Integer.MIN_VALUE;
        for (int i = left;i<=right;i++){
            if (max < nums[i]){
                index = i;
                max = nums[i];
            }
        }
        TreeNode root = new TreeNode(max);
        root.left = helper(nums, left, index-1);
        root.right = helper(nums, index+1, right);
        return root;
    }
}
